package org.example.node;

public class BM00141HasCyclePlus {

    /**
     * Test00141HasCycle
     * 某个有环的链表 ，返回环的入口
     * @param pHead
     * @return
     */
    public ListNode entryNode(ListNode pHead) {
        ListNode fast = pHead;
        ListNode slow = pHead;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow == fast) {
                break;
            }
        }
        if(fast ==null || fast.next == null){
            return null;
        }
        fast = pHead;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}
